package com.seio.edc.common;

import java.io.File;
import java.util.ResourceBundle;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * <p>
 * Project Name: EDC管理系统
 * <br>
 * Description: 装载settings.properties配置文件
 * <br>
 * File Name: Settings.java
 */
public class Settings {
	private static Log log = LogFactory.getLog(Settings.class);
	private static ResourceBundle SETTINGS = null;
	private static ResourceControl rc = new ResourceControl();
	private static long lastModified = 0;
	private static File file = null;

	static {
		SETTINGS = ResourceBundle.getBundle("local/settings", rc);
		try {
			file = new File(Settings.class.getResource("/local/settings.properties").toURI());
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		Thread t = new Thread(new Runnable() {
			@Override
			public void run() {
				while (true) {
					if (file != null) {
						try {
							if (file.lastModified() != lastModified) {
								SETTINGS = ResourceBundle.getBundle("local/settings", rc);
								lastModified = file.lastModified();
							}
							Thread.sleep(60*1000); //休眠60秒，该时间需要大于getTimeToLive(String baseName, Locale locale)设定值。
						} catch (InterruptedException e) {
							log.error(e.getMessage(), e);
						} catch (Throwable t) {
							log.error(t);
						}
					} else {
						log.error("Not found the file of '/local/settings.properties'. ");
						break;
					}
				}
			}
		});
		t.start();
	}

	public static String getStringValue(String key) {
		return SETTINGS.getString(key);
	}

	public static int getIntValue(String key) {
		return Integer.parseInt(SETTINGS.getString(key));
	}
	
	public static boolean getBooleanValue(String key){
		return Boolean.parseBoolean(SETTINGS.getString(key));
	}

}
